module common.gameutil;

import allegro5.allegro;
import allegro5.allegro_image;



enum ColorEffect
{
	ceNone, ceGrayScale, ceBright, ceBlack, ceWhite, ceRed, ceGreen, ceBlue, ceYellow, ceFuchsia
}

public void drawBlend(ALLEGRO_BITMAP* bmp, float dx, float dy, int blendmode)
{
	drawBlendEx(bmp, dx, dy, 0, 0, al_get_bitmap_width(bmp), al_get_bitmap_height(bmp), blendmode);
}

//ssurface + dsurface => dsurface
public void drawBlendEx(ALLEGRO_BITMAP* bmp, float dx, float dy, float sx, float sy, float sw, float sh, int blendmode)
{
	int old_op, old_source, old_dest;
	int new_op, new_source, new_dest;
	
	
	final switch (blendmode)
	{
		case 0: // alphablend
			{
				new_op = ALLEGRO_BLEND_OPERATIONS.ALLEGRO_ADD;
				new_source = ALLEGRO_BLEND_MODE.ALLEGRO_ONE;
				new_dest = ALLEGRO_BLEND_MODE.ALLEGRO_INVERSE_ALPHA;
				break;
			}
			
		case 1: // additive
			{
				new_op = ALLEGRO_BLEND_OPERATIONS.ALLEGRO_ADD;
				new_source = ALLEGRO_BLEND_MODE.ALLEGRO_ONE;
				new_dest = ALLEGRO_BLEND_MODE.ALLEGRO_ONE;
				break;
			}
	}
	
	al_get_blender(&old_op, &old_source, &old_dest);
	
	if ((old_op == new_op) && (old_source == new_source) && (old_dest == new_dest))
	{
		// no need to change the blender
		al_draw_bitmap_region(bmp, sx, sy, sw, sh, dx, dy, 0);
		//al_draw_tinted_bitmap_region(bmp, tint, sx, sy, sw, sh, dx, dy, 0);
	}
	else
	{
		al_set_blender(new_op, new_source, new_dest);
		al_draw_bitmap_region(bmp, sx, sy, sw, sh, dx, dy, 0);
		al_set_blender(old_op, old_source, old_dest);
	}
}

public void DrawEffect(ALLEGRO_BITMAP* bmp, float dx, float dy, ColorEffect eff)
{
	// TODO: finish colour effects
	ALLEGRO_COLOR clr;
	if (eff == ColorEffect.ceNone) return;

	switch (eff)
	{
		//ceGrayScale: peff := @GrayScaleLevel;
		//ceBright: peff := @BrightColorLevel;
		//ceBlack: peff := @BlackColorLevel;
		case ColorEffect.ceBlack: clr = al_map_rgba(50, 50, 50, 255); break;
		//ceWhite: peff := @WhiteColorLevel;
		case ColorEffect.ceRed: clr = al_map_rgba(255, 0, 0, 255); break;
		case ColorEffect.ceGreen: clr = al_map_rgba(0, 128, 0, 255); break;
		case ColorEffect.ceBlue: clr = al_map_rgba(0, 0, 255, 255); break;
		case ColorEffect.ceYellow: clr = al_map_rgba(247, 231, 0, 255); break;
		case ColorEffect.ceFuchsia: clr = al_map_rgba(255, 0, 255, 255); break;
		default: break;
		//else exit;
	}

	al_draw_tinted_bitmap(bmp, clr, dx, dy, 0);
}